﻿using FBroSharp;
using FBroSharp.Const;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.VIP;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace wss测试
{
    public class InitEvent : FBroInitEvent
    {
        //即将处理命令行
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {
            if (Debugger.IsAttached)
            {
                cmd.AppendSwitch("--single-process");
            }

            ////关闭GPU加速
            //cmd.AppendSwitchWithValue("--disable-gpu", "1");
            //cmd.AppendSwitchWithValue("--disable-gpu-compositing", "1");


          

            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + process_type + cmd.IsReadOnly() + cmd.GetProgram());
        }

        //初始化完毕
        public override void OnContextInitialized()
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }

        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + command_line.IsReadOnly() + command_line.GetProgram());
        }

        public string changerWssUrl;
        public string changerWssText;
        public byte[] changeWssData;
        public IFBroSharpDOMWssClient wss;

        public override void ReceiveMainProcessMessage(IFBroSharpBrowser browser, string name, byte[] message)
        {
            if (name == "websocketchangeurl")
            {
                changerWssUrl = Encoding.Default.GetString(message);
            }
            else if (name == "websocketchangetext")
            {
                changerWssText = Encoding.Default.GetString(message);
            }
            else if (name == "websocketsendtext")
            {
                wss.Send(Encoding.Default.GetString(message));
            }
            else if (name == "websocketchangedata")
            {
                changeWssData = new byte[changeWssData.Length];
                message.CopyTo(changeWssData, 0);
            }
            else if (name == "websocketsenddata")
            {
                wss.SendData(message);
            }
            else if (name == "websocketclear")
            {
                changerWssUrl = "";
                changerWssText = "";
                changeWssData = null;
            }
        }

        public override void VIP_OnWebSocketClientClose(IFBroSharpBrowser browser, IFBroSharpFrame frame, IFBroSharpDOMWssClient websocket)
        {
            wss = null;
        }

        public override void VIP_OnWebSocketClientConnect(IFBroSharpBrowser browser, IFBroSharpFrame frame, IFBroSharpDOMWssClient websocket, ref string returl, ref string protocols)
        {
            wss = new FBroSharpDOMWssClient(websocket);

            //将收到的数据反馈给主进程，其实这个如果没有特殊需求建议不要反馈给主进程，直接在渲染进程中处理才是最快和最稳定的，这里处理只是为了演示一下
            var processMessageCon = browser.GetOwnProcessMessageControl();
            if (processMessageCon != null && processMessageCon.IsValid)
            {
                string sendData = String.Format("{0}||{1}||{2}||{3}", MethodBase.GetCurrentMethod().Name, websocket.GetAddress(), returl, protocols);
                processMessageCon.ClientSendByBrowser("websocket", Encoding.Default.GetBytes(sendData));
            }
        }

        public override void VIP_OnWebSocketClientCreate(IFBroSharpBrowser browser, IFBroSharpFrame frame, IFBroSharpDOMWssClient websocket)
        {

        }

        public override bool VIP_OnWebSocketClientMessage(IFBroSharpBrowser browser, IFBroSharpFrame frame, IFBroSharpDOMWssClient websocket, int type, ref byte[] data)
        {
            //将收到的数据反馈给主进程，其实这个如果没有特殊需求建议不要反馈给主进程，直接在渲染进程中处理才是最快和最稳定的，这里处理只是为了演示一下
            var processMessageCon = browser.GetOwnProcessMessageControl();
            if (processMessageCon != null && processMessageCon.IsValid)
            {
                string sendData = String.Format("{0}||{1}||{2}||{3}", MethodBase.GetCurrentMethod().Name, websocket.GetAddress(), type, Encoding.UTF8.GetString(data));
                processMessageCon.ClientSendByBrowser("websocket", Encoding.Default.GetBytes(sendData));
            }
            return false;
        }

        public override bool VIP_OnWebSocketClientSend(IFBroSharpBrowser browser, IFBroSharpFrame frame, IFBroSharpDOMWssClient websocket, int type, ref byte[] data)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + Encoding.Default.GetString(data));

            //数据类型，0为文本，1为ArrayBuffer,2为Blob
            if (type == 0)
            {
                if (changerWssText != null && changerWssText != "")
                {
                    //注意：修改的数据必须是UTF8类型
                    data = Encoding.UTF8.GetBytes(changerWssText);
                }
            }
            else
            {
                if (changeWssData != null && changeWssData.Length > 0)
                {
                    data = changeWssData;
                }
            }
            return false;
        }

    }
}